# -*- coding: utf-8 -*-
# @Time : 2023/9/27 14:43
# @Author : ZH
# @File : 10.py
# @Software: PyCharm
class Solution:
    def isMatch(self, s: str, p: str) -> bool:
        if p == '.*':
            return True
        p_index = 0
        up_index = -1
        if '.' not in p and '*' not in p:
            if s == p:
                return True
            else:
                return False
        while True:
            p_ = p[p_index]
            for index, data in enumerate(s):
                if index < up_index:
                    continue
                if data == p_ or p_ == '.':
                    p_index += 1
                    try:
                        p_ = p[p_index]
                    except:
                        if p_index >= len(p) - 1:
                            if index >= len(s):
                                return True
                            else:
                                return False

                    if p_ == '.' and len(p) > p_index+1 and p[p_index+1] == '*':
                        if len(p) > p_index+2:
                            up_index = self.find_str(s[index:], p[p_index+2])
                            if up_index != -1:
                                p_index += 2
                                p_ = p[p_index+2]
                            else:
                                return False
                if p_index >= len(p)-1:
                    if p_ == '.':
                        return True
                    elif p_ =='*':
                        try:
                            if s[index + 1] == s[index]:
                                return True
                            else:
                                return False
                        except:
                            return False
                    else:
                        return False
            if p[p_index+1] == '*':
                p_index += 2
            else:
                return False
    def find_str(self, s: str, need):
        return s.find(need)

        pass


if __name__ == '__main__':
    so =Solution()
    z = so.isMatch(s="aab", p='c*a*b')
    print(z)
    pass
